<?php



namespace Admin\Controller;



class MemberController extends AdminController {	

    

    public function index(){

    	$username = I('username');



		$obj = M('Member');	



		if ($username != '') {

			$map['username'] = array('like','%'.$username.'%');

		}



		$count = $obj->where($map)->count();

		import("Common.ORG.Page");

		$page = new \Page($count, 20);

		$show = $page->show();

		$list = $obj->where($map)->order('userid desc')->limit($page->firstRow . ',' . $page->listRows)->select();		

		$this->assign('list', $list);

		$this->assign('page', $show);

		$this->assign('username', $username);

		$this->display();

    }



	#编辑

	public function edit(){

		$userid = (int) $_GET['userid'];

		if (!isset ($userid)) {

			$this->error('Parameter error');

		}

		$obj = M('Member');



		$list = $obj->where("userid=$userid")->find();



		if (!$list) {

			$this->error('Information does not exist');

		} else {

			$this->assign('list', $list);

			$this->display();

		}

	}

	

	#更新

	public function update() {



		$obj = D('Member');

        if ($data = $obj->create()) {



        	if ($data['password']=='') {

        		unset($data['password']);

        	}else{

        		$data['password'] = md5($data['password']);

        	}



            if ($obj->save($data)) {

                $state = 'SUCCESS';

                $message = 'Successful operation';

                $url = U('Member/index');

            } else {

                $state = 'Operation failed
';

            }

        }else{

            $state = $obj->getError();

        }

        echo $this->echo_json_str($state,$message,$url);

	}



	#删除

	public function del(){

		$userid=I('post.selectedids');

		if($userid==''){

			$state = "You do not select any information!";

		}else{

			foreach ($userid as $v) {

				$obj = M('Member');

				$where['userid'] = $v;

				$user=$obj->where($where)->find();
				if($user['userType']==1){
					unset($map);
					$map['userid']=$v;
					M("Mentees")->where($map)->setField("del",1);
				}else{
					unset($map);
					$map['userid']=$v;
					M("Mentors")->where($map)->setField("del",1);
				}

				$obj->where($where)->delete();

			}

			$state = 'SUCCESS';

	        $message = 'Successful operation';

	        $url = U('Member/index');

		}

		echo $this->echo_json_str($state,$message,$url);

	}

}